{% extends  'common_project_tpl.html' %}
{% block css %}
    <style>
        textarea {
            resize: vertical;
        }

        .panel-body {
            padding: 0;
            display: flex;
            flex-direction: row;
            justify-content: left;
            align-items: flex-start;
            flex-wrap: wrap;
        }

        .panel-body > .item {
            border-radius: 6px;
            width: 228px;
            border: 1px solid #dddddd;
            margin: 20px 10px;

        }

        .panel-body > .item:hover {
            border: 1px solid #f0ad4e;
        }

        .panel-body > .item > .title {
            height: 104px;
            color: white;
            display: flex;
            justify-content: center;
            align-items: center;
            border-top-left-radius: 6px;
            border-top-right-radius: 6px;
            font-size: 15px;
            text-decoration: none;
        }

        .panel-body > .item > .info {
            padding: 10px 10px;

            display: flex;
            justify-content: space-between;

            border-bottom-left-radius: 6px;
            border-bottom-right-radius: 6px;
            color: #8c8c8c;

        }

        .panel-body > .item > .info a {
            text-decoration: none;
        }

        .panel-body > .item > .info .fa-star {
            font-size: 18px;
        }

        .color-radio label {
            margin-left: 0;
            padding-left: 0;
        }

        .color-radio input[type="radio"] {
            display: none;
        }

        .color-radio input[type="radio"] + .cycle {
            display: inline-block;
            height: 25px;
            width: 25px;
            border-radius: 50%;
            border: 2px solid #dddddd;
        }

        .color-radio input[type="radio"]:checked + .cycle {
            border: 2px solid black;
        }
    </style>
{% endblock %}
{% block content %}
    <div class="container-fluid">
        <a href="#" id="create-project" class="btn btn-primary" data-toggle="modal" data-target="#projectModal"
           style="margin: 20px;">
            <i class="fa fa-plus" aria-hidden="true"/></i> 新建项目
        </a>

        <div class="panel panel-warning">
            <div class="panel-heading">
                <h3 class="panel-title"><i class="fa fa-star" aria-hidden="true"/></i> 星标项目</h3>
            </div>
            <div class="panel-body" id="star_list">
            </div>
        </div>
        <div class="panel panel-info">
            <div class="panel-heading">
                <h3 class="panel-title"><i class="fa fa-navicon" aria-hidden="true"/></i> 我的项目</h3>
            </div>
            <div class="panel-body" id="create_list">
            </div>
        </div>
        <div class="panel panel-success">
            <div class="panel-heading">
                <h3 class="panel-title"><i class="fa fa-arrow-down" aria-hidden="true"/></i> 参与项目</h3>
            </div>
            <div class="panel-body" id="join_list">
            </div>
        </div>
    </div>
    <!-- Modal -->
    <div class="modal fade" id="projectModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">新建项目</h4>
                </div>
                <div class="modal-body">
                    <form id="project-form">
                        {% csrf_token %}
                        {% for field in form %}
                            <div class="form-group">
                                <label for={{ field.id_for_label }}>{{ field.label }}</label>
                                {{ field }}
                                <span class="error-msg"></span>
                            </div>
                        {% endfor %}
                        <span id="non_field_errors" class="error-msg"></span>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="create">新建</button>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block content2 %}
    <span id="dashbord_url_temp" style="display: none;">{% url 'main:dashboard' project=1 %}</span>
{% endblock %}

{% block js %}
    <script>
        $(function () {
            {#函数的调用#}
            createProjectBtn();
            getProjectList();
        });

        function createProjectBtn() {
            $('#create').click(function () {
                $('.error-msg').empty();
                let projet_data = $('#project-form').serialize();
                $.ajax({
                    url: "{% url 'main:api_project' %}",
                    type: "post",
                    data: projet_data,
                    dataType: "JSON",
                    success: function (res) {
                        console.log(res)
                        if (res.ret === 0) {
                            {#console.log(res)#}
                            location.reload(); // 刷新页面
                        } else { //循环此字典每次循环执行函数
                            $.each(res.errors, function (name, data) {
                                $('#id_' + name).next().text(data[0])
                                if (name === 'non_field_errors') {
                                    $('#non_field_errors').text(data[0])
                                }
                            })
                        }
                    },
                    error: function (res) {
                        console.log(res.responseJSON)
                        $.each(res.responseJSON.errors, function (name, data) {
                            $('#id_' + name).next().text(data[0])
                            if (name === 'non_field_errors') {
                                $('#non_field_errors').text(data[0])
                            }
                        })
                    }
                })
            })
        }

        function getProjectList() {
            $.ajax({
                url: "{% url 'main:api_project' %}",
                type: "get",
                dataType: "JSON",
                success: function (res) {
                    console.log(res)
                    $.each(res, function (name, data) {
                        let container = $("#" + name + "_list");
                        $(container).empty()
                        let star_color = '#d5d5d5'
                        if (name === 'star') {
                            star_color = '#f0ad4e'
                        }
                        console.log(name)
                        if (name !== 'ret') {
                            $.each(data, function (num, pro) {
                                console.log(pro)
                                let dashbord_url = $('#dashbord_url_temp').text().replace('/1/', `/${pro.id}/`)
                                let star = "{% url 'main:api_project_star'%}" + "?project=" + pro.id
                                let htmlCode = `
                                    <div class="item">
                                        <a href="${dashbord_url}" class="title" style="background-color: ${pro.color_code}">${pro.project_title}</a>
                                        <div class="info">
                                            <div>
                                                <a href="${star}">
                                                    <i class="fa fa-star" aria-hidden="true" style="color: ${star_color};"></i>
                                                </a>
                                                <span>${pro.creator_name}</span>
                                            </div>
                                            <div>
                                                <i class="fa fa-user-o" aria-hidden="true"></i>
                                                <span>${pro.workers_num}</span>
                                            </div>
                                        </div>
                                    </div>`
                                container.append(htmlCode)

                            })
                        }
                    })
                }
            })

        }
    </script>
{% endblock %}